<?php

namespace App\Http\Resources;


use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class MsgInfoResource extends JsonResource{
    public function toArray($request)
    {
        //获取发送人姓名
        $send_name=User::query()->select('username')->where(['id'=>$this->send_id])->first();
        if($send_name){
            $send_name=$send_name->username;
        }else{
            $send_name='';
        }
        $recevier_name=User::query()->select('username')->where(['id'=>$this->recevier_id])->first();
        if($recevier_name){
            $recevier_name=$recevier_name->username;
        }else{
            $recevier_name='';
        }
        return [
            'id'=>$this->id,
            'title'=>$this->msg_title,
            'msg_content'=>$this->msg_content,
            'send_name'=>$send_name,
            'recevier_name'=>$recevier_name,
            'send_date'=>$this->send_date,
            'read_date'=>$this->read_date,
            'is_read'=>$this->is_read==0 ?'未读':'已读'

        ];
    }
}
